package admin

import (
	"context"
	"grpc.getaway.adminspro.padmins.com/internal/rpc/module/admin/adminLogic"
	"grpc.getaway.adminspro.padmins.com/internal/rpc/module/admin/roleLogic"
	"grpc.getaway.adminspro.padmins.com/internal/rpc/utils/database"
	"grpc.getaway.adminspro.padmins.com/pkg/businessCommonQuery"
	"grpc.getaway.adminspro.padmins.com/pkg/config/businessError"
	"grpc.getaway.adminspro.padmins.com/pkg/libs/utils"
	pbAdmin "grpc.getaway.adminspro.padmins.com/pkg/proto/admin"
)

func (rpc *admin) AdminRoleDetail(ctx context.Context, pb *pbAdmin.AdminRoleDetailReq) (*pbAdmin.AdminRoleDetailResp, error) {

	Db := database.Db.GetDefaultDb(ctx)

	row, dbErr := Db.Find("select * from admin_role where id=?", pb.Id)
	if dbErr != nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.DbError(dbErr))
	}
	if row == nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.NotFound)
	}

	if pb.OperaterId != 0 {
		roleIds := adminLogic.GetChildrenRoleIds(ctx, pb.OperaterId, true)
		if !utils.InArray(pb.Id, roleIds) {
			return nil, businessError.ErrorInfoToRpcError(businessError.NoPermission)
		}
	}

	resourceRows, dbErr := Db.Query("select resource_id from admin_role_resource_access where role_id=?", row["id"])

	if dbErr != nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.DbError(dbErr))
	}

	resourceIds := utils.ArrayColumn(resourceRows, "resource_id")
	resources := []map[string]interface{}{}

	if utils.InArray(0, resourceIds) {
		resources, dbErr = Db.Query("select * from admin_resource order by weigh,path")
	} else {
		resources, dbErr = Db.Query("select * from admin_resource where id in (?) order by weigh,path", resourceIds)
	}

	if dbErr != nil {
		return nil, businessError.ErrorInfoToRpcError(businessError.DbError(dbErr))
	}

	resourceIds = utils.ArrayColumn(resources, "id")
	businessCommonQuery.ListFormat(roleLogic.List(), row)

	resp := &pbAdmin.AdminRoleDetailResp{}
	resp.Row = &pbAdmin.AdminRoleDetailResp_AdminRole{
		Id:         row["id"].(int64),
		Pid:        row["pid"].(int64),
		Name:       row["name"].(string),
		Status:     int32(row["status"].(int64)),
		UpdateTime: row["update_time"].(string),
		CreateTime: row["create_time"].(string),
		StatusText: row["status_text"].(string),
		Resources:  make([]*pbAdmin.AdminResource, len(resources)),
	}

	for i, resource := range resources {
		resp.Row.Resources[i] = &pbAdmin.AdminResource{
			Id:         resource["id"].(int64),
			Pid:        resource["pid"].(int64),
			Type:       int32(resource["type"].(int64)),
			Path:       resource["path"].(string),
			Resource:   resource["resource"].(string),
			Title:      resource["title"].(string),
			Icon:       resource["icon"].(string),
			Remark:     resource["remark"].(string),
			Badge:      resource["badge"].(string),
			Weigh:      resource["weigh"].(int64),
			Status:     int32(resource["weigh"].(int64)),
			CreateTime: resource["create_time"].(string),
			UpdateTime: resource["update_time"].(string),
			Name:       resource["title"].(string),
		}
		if !utils.InArray(resource["pid"], resourceIds) {
			resp.Row.Resources[i].Pid = 0
		}
	}
	return resp, nil
}
